package com.lmx.repoDomain.tmodel;

import io.katharsis.resource.annotations.JsonApiIncludeByDefault;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import io.katharsis.resource.annotations.JsonApiToOne;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * 食物
 *
 * @author ytche
 * @date 2017年8月30日
 *
 */
@Entity
@Table(name = "tnb_food")
@JsonApiResource(type = "food")
public class Food extends BaseEntity {

	private static final long serialVersionUID = 1L;
	private String name;
	private Integer price;
	private Set<FoodSign> signs;
	//private Set<CustomerFoodPlan> plans;
	private Dicttype type;// 字典 主食 菜 汤
	private String picture;// 图片
    
	@Column(name = "picture")
	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.JOIN)
	@JsonApiToOne
	@JsonApiIncludeByDefault
	@JoinColumn(name = "type_id")
	public Dicttype getType() {
		return type;
	}

	public void setType(Dicttype type) {
		this.type = type;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "price")
	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@JoinTable(name = "rel_food_type", joinColumns = { @JoinColumn(name = "food_id") }, inverseJoinColumns = {
			@JoinColumn(name = "type_id") })
	@Fetch(FetchMode.JOIN)
	@JsonApiToMany
	@JsonApiIncludeByDefault
	public Set<FoodSign> getSigns() {
		return signs;
	}

	public void setSigns(Set<FoodSign> signs) {
		this.signs = signs;
	}

//	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, mappedBy = "foods")
//	@JsonApiToMany
//	@JsonApiLookupIncludeAutomatically
//	public Set<CustomerFoodPlan> getPlans() {
//		return plans;
//	}
//
//	public void setPlans(Set<CustomerFoodPlan> plans) {
//		this.plans = plans;
//	}

	@Override
	public boolean equals(Object obj) {
		if (super.equals(obj)) {
			return true;
		}
		if (obj instanceof Food) {
			return ((Food) obj).getId().equals(this.getId());
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.getId().intValue();
	}
}
